<?php 

namespace app\common\shop;

use think\Db;
use think\Model;
use think\model\concern\SoftDelete;
use app\common\shop\SpecModel;


class SpecListModel extends model {
//    use SoftDelete;//开启了软删除
//    protected  $deleteTime= 'del_time';
    // 完整数据表名
    protected $table = 'shop_spec_list';

    /**
     * [建表]
     * @return
     */
    public static function createTable(){
        return Db::query("CREATE TABLE If Not Exists `shop_spec_list` (
             `id` int(11) NOT NULL AUTO_INCREMENT,
             `goodsid` int(11) NOT NULL COMMENT '商品id',
             `key` varchar(255) NOT NULL COMMENT '标识',
             `price` decimal(22,2) NOT NULL DEFAULT '0.00' COMMENT '价格',
             `total` int(11) NOT NULL DEFAULT '0' COMMENT '库存',
             PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='规格价格表'
        ");
    }

    /**
     * [添加]
     * @return
    */
    public static function add($data){
        return self::insertGetId($data);
    }
    /**
     * [修改]
     * @return
    */
    public static function edit($data,$id){
        return self::where('id',$id)->update($data);
    }  

    /**
     * [处理 修改/添加 数据]
     * @param
     * @return
     */
    public static function dispoSespecList($data=[],$goodsid=0){

        $allsid = '';
        $d = [];
        foreach ($data as $key => $value) {
            // 规格数据
            $spec_list = [
                'goodsid'=>$goodsid,
                'price'=>$value['price'],
                'total'=>$value['total'],
                'team_prices'=>$value['team_prices'],
                'key'=>$key,
            ];

            // 是否已存在
            $spec_info = self::where('goodsid',$goodsid)->where('key',$key)->find();

            if($spec_info){

                self::edit($spec_list,$spec_info['id']);
                $sid = $spec_info['id'];
            }else{
                $sid = self::add($spec_list);
            }

            $allsid .= $sid.',';
            $y = explode('_', $key);
            foreach ($y as $k => $val) {
                if(!in_array($val,$d)){
                    // 已经改过
                    $info_spec = SpecModel::getInfo($val);
                    if($info_spec['goodsid'] == 0){
                        SpecModel::whereOr('id',$info_spec['id'])->whereOr('id',$info_spec['pid'])->setField('goodsid',$goodsid);
                    }
                }
            }
        }

        $allsid = trim($allsid,',');
        self::where('goodsid',$goodsid)->where('id','not in',$allsid)->delete();// 删除规格
        // 给规格加上商品id
        return true;
    }

    /**
     * [获取单条信息]
     * @return
    */
    public static function getInfo($id){
        return self::where('id',$id)->find();
    } 
    /**
     * [获取商品规格]
     * @return
    */
    public static function getList($goodsid = 0){
        return self::where('goodsid',$goodsid)->select();
    }

    /**
     * [获取规格价格]
     * @param
     * @return
     */
    public static function getSpecList( $key,$goodsid ){
        return self::where('key',$key)->where('goodsid',$goodsid)->find();
    }
}